package Prefix_and;

import java.util.HashMap;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-01-25
 * Time:21:18
 * Text：和为 K 的子数组
 */
public class Text5 {

    public static int subarraySum2(int[] nums, int k){
        int[] num2=new int[nums.length];
        int count=0;
        for(int i=0;i<nums.length;i++){
            for(int c=i;c<nums.length;c++){
                num2[i]+=nums[c];
                if(num2[i]==k){
                    count++;
                }
            }
        }
        return count;
    }
    public static int subarraySum(int[] nums, int k) {
        HashMap<Integer,Integer> map1=new HashMap<>();
        map1.put(0,1);
        int num=0,ret=0;
        for(int i=0;i<nums.length;i++){
            num+=nums[i];
            ret+=map1.getOrDefault(num-k,0);
            map1.put(num,(map1.getOrDefault(num,0)+1));
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] array=new int[]{1,-1,0};
        subarraySum(array,0);
    }
}
